﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Hypermarket_Serial_Port
{
    class ActivationManager
    {
        private static ActivationManager mInstance;
        private DBManager mDBManager;
        private ActivationManager() 
        {
            mDBManager = DBManager.getInstance();
        }

        public static ActivationManager getInstance()
        {
            if (mInstance == null)
            {
                mInstance = new ActivationManager();
            }
            return mInstance;
        }

        public List<string> GetActivedCashierRegistorList(Dictionary<string, int> cashierRegister)
        {

            Dictionary<string, int> newCashierRegisters;
            List<string> machineIds = mDBManager.GetActivedCashierRegisterIDs(out newCashierRegisters);

            UpdateCashierRegister(cashierRegister, newCashierRegisters);

            return machineIds;
        }

        private static void UpdateCashierRegister(Dictionary<string, int> cashierRegister, Dictionary<string, int> newCashierRegisters)
        {
            List<string> machineToBeRemoved = new List<string>();
            foreach (KeyValuePair<string, int> pair in cashierRegister)
            {
                if (!newCashierRegisters.ContainsKey(pair.Key))
                {
                    machineToBeRemoved.Add(pair.Key);
                }
            }
            foreach (string id in machineToBeRemoved)
            {
                cashierRegister.Remove(id);
            }
            foreach (KeyValuePair<string, int> pair in newCashierRegisters)
            {
                if (!cashierRegister.ContainsKey(pair.Key))
                {
                    cashierRegister.Add(pair.Key, pair.Value);
                }
            }
        }

        public List<string> GetActivedPriceTagList(Dictionary<string, int> priceTag)
        {
            Dictionary<string, int> newPriceTags;
            List<string> machineIds = mDBManager.GetActivedPriceTagIDs(out newPriceTags);
            UpdatePriceTag(priceTag, newPriceTags);
            return machineIds;
        }

        private static void UpdatePriceTag(Dictionary<string, int> priceTag, Dictionary<string, int> newPriceTags)
        {
            List<string> machineToBeRemoved = new List<string>();
            foreach (KeyValuePair<string, int> pair in priceTag)
            {
                if (!newPriceTags.ContainsKey(pair.Key))
                {
                    machineToBeRemoved.Add(pair.Key);
                }
            }
            foreach (string id in machineToBeRemoved)
            {
                priceTag.Remove(id);
            }
            foreach (KeyValuePair<string, int> pair in newPriceTags)
            {
                if (!priceTag.ContainsKey(pair.Key))
                {
                    priceTag.Add(pair.Key, pair.Value);
                }
            }
        }

        public void ReloadData()
        {
            mDBManager.ReloadDataSet();
        }

        public void Deactive(List<string> cashierRegister, List<string> priceTag)
        {
            mDBManager.DeactiveMachine(cashierRegister, priceTag);
        }
    }
}
